package com.xxh.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxh.food.common.entity.Food;

import com.xxh.food.common.entity.User;
import com.xxh.food.common.param.FoodPARAM;

import com.xxh.food.mapper.FoodMapper;
import com.xxh.food.service.FoodService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    @Resource
    private FoodMapper foodMapper;


    @Override
    public Page<Food> findFoodByPage(Integer pageNum, Integer pageSize, FoodPARAM foodPARAM) {
        Page<Food> page = new Page<Food>(pageNum,pageSize);
        QueryWrapper<Food> qw = new QueryWrapper<>();
        if(foodPARAM == null){
            foodMapper.selectPage(page,qw);
        }

        if(StringUtils.isNotEmpty(foodPARAM.getId())){
            qw.like("id",foodPARAM.getId());
        }

        if(StringUtils.isNotEmpty(foodPARAM.getName())){
            qw.like("name",foodPARAM.getName());
        }

        if(StringUtils.isNotEmpty(foodPARAM.getType())){
            qw.like("type",foodPARAM.getType());
        }

        if(StringUtils.isNotEmpty(foodPARAM.getRestaurantAddress())){
            qw.like("restaurantAddress",foodPARAM.getRestaurantAddress());
        }

        if(StringUtils.isNotEmpty(foodPARAM.getContent())){
            qw.like("content",foodPARAM.getContent());
        }

        if(StringUtils.isNotEmpty(foodPARAM.getImg())){
            qw.like("img",foodPARAM.getImg());
        }

        if(StringUtils.isNotEmpty(foodPARAM.getRestaurantName())){
            qw.like("restaurantName",foodPARAM.getRestaurantName());
        }

        return foodMapper.selectPage(page,qw);
    }

    @Override
    public int foodNum() {
        QueryWrapper<Food> qw = new QueryWrapper<>();
        return Math.toIntExact(foodMapper.selectCount(qw));
    }
}
